package com.rickpan.dto.siliconflow;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;

/**
 * 硅基流动AI API请求DTO
 */
public class SiliconFlowRequest {

    private String model;
    private Boolean stream;
    private Double temperature;
    
    @JsonProperty("top_p")
    private Double topP;
    
    private List<SiliconFlowMessage> messages;

    // 构造函数
    public SiliconFlowRequest() {
        this.messages = new ArrayList<>();
    }

    // Builder模式
    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private String model = "THUDM/GLM-4.1V-9B-Thinking";
        private Boolean stream = false;
        private Double temperature = 0.8;
        private Double topP = 0.8;
        private List<SiliconFlowMessage> messages = new ArrayList<>();

        public Builder model(String model) {
            this.model = model;
            return this;
        }

        public Builder stream(Boolean stream) {
            this.stream = stream;
            return this;
        }

        public Builder temperature(Double temperature) {
            this.temperature = temperature;
            return this;
        }

        public Builder topP(Double topP) {
            this.topP = topP;
            return this;
        }

        public Builder messages(List<SiliconFlowMessage> messages) {
            this.messages = messages;
            return this;
        }

        public Builder addMessage(String role, String content) {
            this.messages.add(new SiliconFlowMessage(role, content));
            return this;
        }

        public Builder addSystemMessage(String content) {
            return addMessage("system", content);
        }

        public Builder addUserMessage(String content) {
            return addMessage("user", content);
        }

        public Builder addUserMessageWithImage(String text, String imageBase64) {
            List<SiliconFlowMessage.ContentItem> contentItems = new ArrayList<>();

            // 添加图片内容
            if (imageBase64 != null && !imageBase64.isEmpty()) {
                // 确保base64格式正确
                String imageUrl = imageBase64.startsWith("data:") ? imageBase64 : "data:image/png;base64," + imageBase64;
                contentItems.add(SiliconFlowMessage.ContentItem.imageUrl(imageUrl));
            }

            // 添加文本内容
            if (text != null && !text.isEmpty()) {
                contentItems.add(SiliconFlowMessage.ContentItem.text(text));
            }

            this.messages.add(new SiliconFlowMessage("user", contentItems));
            return this;
        }

        public SiliconFlowRequest build() {
            SiliconFlowRequest request = new SiliconFlowRequest();
            request.model = this.model;
            request.stream = this.stream;
            request.temperature = this.temperature;
            request.topP = this.topP;
            request.messages = this.messages;
            return request;
        }
    }

    // Getter 和 Setter 方法
    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Boolean getStream() {
        return stream;
    }

    public void setStream(Boolean stream) {
        this.stream = stream;
    }

    public Double getTemperature() {
        return temperature;
    }

    public void setTemperature(Double temperature) {
        this.temperature = temperature;
    }

    public Double getTopP() {
        return topP;
    }

    public void setTopP(Double topP) {
        this.topP = topP;
    }

    public List<SiliconFlowMessage> getMessages() {
        return messages;
    }

    public void setMessages(List<SiliconFlowMessage> messages) {
        this.messages = messages;
    }

    @Override
    public String toString() {
        return "SiliconFlowRequest{" +
                "model='" + model + '\'' +
                ", stream=" + stream +
                ", temperature=" + temperature +
                ", topP=" + topP +
                ", messages=" + messages +
                '}';
    }
}
